<template>
  <div class="login-wrapper">
    <div class="login-wrapper__box">
      <h3 class="title">后台登录</h3>
      <el-form :model="form" :rules="rules" ref="formRef" @submit.native.prevent v-loading="loading">
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username" placeholder="请输入账号" @keyup.enter.native="login" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码" @keyup.enter.native="login" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="login" :loading="loading">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { UserActionsEnum } from '../../_/vue/store/user'
export default {
  data () {
    return {
      loading: false,
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    ...mapActions({
      loginAction: UserActionsEnum.LOGIN
    }),
    login () {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.loading = true
          this.loginAction(this.form).then(() => {
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
        return false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-wrapper {
  &__box {
    width: 380px;
    padding: 15px;
    background-color: #fff;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 1px 1px 10px 1px rgba(0, 0, 0, 0.15);

    .title {
      text-align: center;
      margin: 0;
      padding: 0;
    }
  }
}
</style>
